package fr.emse.tatiana.filter;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

public class FilterParameter {
	
	private Integer number;
	private String label;
	private String defaultvalue;
	private String returnvalue;
	private String type;

	/**
	 * @param num The position in the stack for this variable
	 * @param label Information for the user for this variable
	 * @param defaultvalue Give a value by default - or a value to approximate for files
	 * @param type filename for files, TODO z later string, integer, boolean, others for validation
	 */
	public FilterParameter(Integer num, String label, String defaultvalue, String type) {
		this.number = num;
		this.label = label;
		this.defaultvalue = defaultvalue;
		this.type = type;
		this.returnvalue = defaultvalue;
	}

	public String getDefaultvalue() {
		return defaultvalue;
	}

	public String getLabel() {
		return label;
	}

	public Integer getNumber() {
		return number;
	}

	public String getValue() {
		return returnvalue;
	}

	public String getType() {
		return type;
	}
	
	public long getVersionNumber() {
		if (FilterExecuter.ftype.equals(type)) {
			String pathstring = FilterablePathFactory.getFilterablePath(returnvalue);
			IPath path = new Path(pathstring);
			String name = path.lastSegment();
			if (name.startsWith(".")) {
				name = name.substring(1, name.length()-4);
				path = path.removeLastSegments(1).append(new Path(name));
			}
			IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
			return file.getModificationStamp();
		}
		return 0;
	}

	public String getFilterableValue() {
		if (FilterExecuter.ftype.equals(type)) {
			return FilterablePathFactory.getFilterablePath(returnvalue);
		} else {
			return returnvalue; 
		}
	}
	
	public void setReturnvalue(String returnvalue) {
		this.returnvalue = returnvalue;
	}
}
